﻿
#region NameSpace
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;
#endregion

public partial class ucRole : System.Web.UI.UserControl
{
    UserManager _userManager = new UserManager();

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        btnAddEditPermission.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnAddEditPermission.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnSave.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSave.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnDeleteRole.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnDeleteRole.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnEditRole.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnEditRole.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnSearch.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSearch.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnBack.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnBack.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnDeleteRole.Attributes.Add("OnClick", "return confirmDelete()");
        if (!IsPostBack)
        {
            upnlPermissions.Visible = false;
            Session["UserManager_RoleSearch"] = "Latest";
            SearchRoles();           
        }
    }
   
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtSearch.Text == string.Empty)
            Session["UserManager_RoleSearch"] = "BlankSearch";
        else
            Session["UserManager_RoleSearch"] = txtSearch.Text;
        SearchRoles();   
    }

    protected void gvwRoles_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwRoles.PageIndex = e.NewPageIndex;
        SearchRoles();
    }

    protected void btnEditRole_Click(object sender, EventArgs e)
    {
        if (CheckSelection())
        {
            Session["UserManager_Role"] = "Role Edit";
            Session["UserManager_Control"] = "UserManager/ucAddRole.ascx";
            Response.Redirect("BusinessProcessAutomation.aspx");
        }
    }

    protected void btnDeleteRole_Click(object sender, EventArgs e)
    {
        Delete();
    }

    protected void btnAddEditPermission_Click(object sender, EventArgs e)
    {
        if (CheckSelection())
        {
            upnlRoles.Visible = false;
            upnlPermissions.Visible = true;
            lblRole.Text = "Role : " + Session["UserManager_Role"].ToString();
            ShowPermissions();
            FillDDL();
        }

    }

    protected void btnAddRole_Click(object sender, EventArgs e)
    {
        Session["UserManager_Role"] = "Role Add";
        Session["UserManager_Control"] = "UserManager/ucAddRole.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        upnlRoles.Visible = true;
        upnlPermissions.Visible = false;
    }

    protected void gvwPermissions_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataSet ds = new DataSet();
            ds = _userManager.GetData("PermissionList", string.Empty);
            DropDownList ddlGrid = (DropDownList)e.Row.FindControl("ddlPermission");
            ddlGrid.DataSource = ds;
            ddlGrid.DataValueField = "PL_PermissionID";
            ddlGrid.DataTextField = "PL_PermissionName";
            ddlGrid.DataBind();
            ddlGrid.Items.Insert(0, "Select");
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        } 
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";

        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";

    }

    protected void ddlUtilityGroup_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        _userManager.SearchRolePermission(Session["UserManager_RoleID"].ToString(), ddlUtilityGroup.SelectedValue, out ds);
        gvwPermissions.DataSource = ds;
        gvwPermissions.Columns[0].Visible = true;
        gvwPermissions.DataBind();
        gvwPermissions.Columns[0].Visible = false;
    }

    protected void GetDetails(object sender, EventArgs e)
    {
        DropDownList ddlGrid = (DropDownList)sender;
        GridViewRow gvRow = (GridViewRow)ddlGrid.NamingContainer;
        Session["UserManager_utilityID"] = gvRow.Cells[0].Text;
        Session["UserManager_PermissionID"] = ddlGrid.SelectedValue;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        AssignPermission();
    }

    protected void gvwRoles_RowCreated(object sender, GridViewRowEventArgs e)
    {

    }

    protected void gvwRoles_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //Find the checkbox control in header and add an attribute
            ((CheckBox)e.Row.FindControl("cbSelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("cbSelectAll")).ClientID + "')");
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        } 
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";

        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }

    protected void ShowUsers(object sender, EventArgs e)
    {
        LinkButton lnbGridRoles = (LinkButton)sender;
        GridViewRow gvRow = (GridViewRow)lnbGridRoles.NamingContainer;
        string roleID = gvRow.Cells[1].Text;
        string condition = "T1.UD_UserPersonalID = T2.UM_UserID and T2.UM_RoleID = '" + roleID + "'";

        DataSet ds = _userManager.GetData("UserDetails", "UserRolesMap", condition);
        if (ds.Tables[0].Rows.Count == 0)
        {
            Response.Write("<script>alert('No User has Been Assigned This Role !')</script>");
            return;
        }
        gvwUsers.Visible = true;
        gvwUsers.DataSource = ds;
        gvwUsers.DataBind();

        lnbBack.Visible = true;
        gvwRoles.Visible = false;
        btnEditRole.Enabled = false;
        btnDeleteRole.Enabled = false;
        btnAddEditPermission.Enabled = false;
    }

    protected void lnbBack_Click(object sender, EventArgs e)
    {
        lnbBack.Visible = false;
        gvwUsers.Visible = false;
        gvwRoles.Visible = true;
        btnEditRole.Enabled = true;
        btnDeleteRole.Enabled = true;
        btnAddEditPermission.Enabled = true;
    }

    protected void gvwUsers_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    #endregion

    #region Methods
    bool CheckSelection()
    {
        int count = 0, selectedRow = -1;

        for (int i = 0; i < gvwRoles.Rows.Count; i++)
        {
            CheckBox chkGrid = (CheckBox)(gvwRoles.Rows[i].Cells[0].FindControl("chkEditDelete"));
            if (chkGrid.Checked == true)
            {
                count++;
                selectedRow = i;
            }
        }

        if (count == 0)
        {
            Response.Write("<script>alert('Select a Role')</script>");
            return false;
        }
        else if (count > 1)
        {
            Response.Write("<script>alert('Select Single Role')</script>");
            return false;
        }
        Session["UserManager_RoleID"] = gvwRoles.Rows[selectedRow].Cells[1].Text;
        LinkButton lnbGrid = (LinkButton)(gvwRoles.Rows[selectedRow].Cells[2].FindControl("lnbRole"));
        Session["UserManager_Role"] = lnbGrid.Text;//gvwRoles.Rows[selectedRow].Cells[2].Text;
        return true;

    //    Session["UserManager_RoleID"] = gvwRoles.Rows[selectedRow].Cells[1].Text;
    //    Session["UserManager_Role"] = gvwRoles.Rows[selectedRow].Cells[2].Text;
    //    return true;
    }

    void SearchRoles()
    {
        try
        {       
            DataSet ds = new DataSet();
            _userManager.RoleSearch(Session["UserManager_RoleSearch"].ToString(), out ds);
          
            gvwRoles.DataSource = ds;
            gvwRoles.Columns[1].Visible = true;
            gvwRoles.DataBind();
            gvwRoles.Columns[1].Visible = false;
           
            if (ds.Tables[0].Rows.Count == 0)
            {
                Response.Write("<script>alert('No Roles Found !')</script>");
            }

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Search Roles Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }
  
    void Delete()
    {       
        int index = 0;
        string roleID;
        string[] roleIDs = new string[gvwRoles.Rows.Count];
        for (int i = 0; i < gvwRoles.Rows.Count; i++)
        {
            CheckBox chkGrid = (CheckBox)(gvwRoles.Rows[i].Cells[0].FindControl("chkEditDelete"));
            if (chkGrid.Checked == true)
            {
                roleIDs[index] = gvwRoles.Rows[i].Cells[1].Text;
                index++;
            }
        }

        if (index == 0)
        {
            Response.Write("<script>alert('Select Role')</script>");
            return;
        }    

        string conditionDeleteRole = "UR_RoleID='" + roleIDs[0] + "'";
        string conditionDeleteRoleMap = "UM_RoleID ='" + roleIDs[0] + "'";
        string conditionDeletePermissionMap = "PM_ActorType = 2 and (PM_ActorID = '" + roleIDs[0] + "'";
        for (int i = 1; i < index; i++)
        {
            conditionDeleteRole = conditionDeleteRole + "or UR_RoleID='" + roleIDs[i] + "'";
            conditionDeleteRoleMap = conditionDeleteRoleMap + "or UM_RoleID='" + roleIDs[i] + "'";
            conditionDeletePermissionMap = conditionDeletePermissionMap + "or PM_ActorID='" + roleIDs[i] + "'";
        }
        conditionDeletePermissionMap = conditionDeletePermissionMap + ")";
        _userManager.RoleDelete(conditionDeleteRoleMap, conditionDeletePermissionMap, conditionDeleteRole);


        Response.Write("<script>alert('Deleted Successfully !')</script>");
        SearchRoles();
    }

    void ShowPermissions()
    {
        try
        {
            DataSet ds = new DataSet();
            _userManager.SearchRolePermission(Session["UserManager_RoleID"].ToString(), "", out ds);

            gvwPermissions.DataSource = ds;
            gvwPermissions.Columns[0].Visible = true;
            gvwPermissions.DataBind();
            gvwPermissions.Columns[0].Visible = false;

            if (ds.Tables[0].Rows.Count == 0)
            {
                Response.Write("<script>alert('No Permissions Assigned !')</script>");
            }

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Show Permissions Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }        
    }

    void FillDDL()
    {
        try
        {
            DataSet ds = new DataSet();
            ds = _userManager.GetData("UtilityGroup", "");

            ddlUtilityGroup.DataSource = ds;
            ddlUtilityGroup.DataTextField = "UG_GroupName";
            ddlUtilityGroup.DataValueField = "UG_GroupID";
            ddlUtilityGroup.DataBind();
            ddlUtilityGroup.Items.Insert(0, "Select");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Fill DropDownList Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }

    void AssignPermission()
    {
        try
        {
            UserPermission permission = new UserPermission();
            permission.ActorType = "2";
            permission.ActorID = Session["UserManager_RoleID"].ToString();
            permission.PermissionID = Session["UserManager_PermissionID"].ToString();
            permission.UtilityID = Session["UserManager_utilityID"].ToString();
            if (!_userManager.AssignPermission(permission))
            {
                Response.Write("<script>alert('Already Exist')</script>");
            }
            else
                Response.Write("<script>alert('Assigned Successfully !')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Assign Permission Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }
    #endregion

    protected void gvwRoles_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvwUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        }
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";
        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }
}
